package com.landeqing.utils;

import com.landeqing.controller.ImportAndEcportExcleController;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;


public class TooPoi {
    private static Logger log = LoggerFactory.getLogger(ImportAndEcportExcleController.class);
    public static String writeExcel(SXSSFWorkbook wb, String name) {
        String filePath = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "exports";
        File f = new File(filePath);
        if (!f.exists()) {
            f.mkdir();
        }
        String path = filePath + File.separator + name +new Date().getTime() + ".xls";
        FileOutputStream os = null;
        try {
            File file = new File(path);
            if(!file.exists()){
                file.createNewFile();
            }
            os = new FileOutputStream(file);
            wb.write(os);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return path;

    }
    @SuppressWarnings("unused")
    public static String export(String templatePath) {
        // 导出文件存放目录
        String filePath = PathKit.getWebRootPath() + File.separator + "exportFile";
        File fileDir = new File(filePath);
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        // 导出文件路径
        String path = filePath + File.separator + new Date().getTime() + ".xls";
        XSSFWorkbook wb = null;
        SXSSFWorkbook swb = null;
        FileOutputStream os = null;
        try {
            // 1.载入模板
            wb = new XSSFWorkbook(new File(templatePath)); // 初始化HSSFWorkbook对象
            wb.setSheetName(0, "用户信息导出");
            Sheet sheet = wb.getSheetAt(0); // wb.createSheet("监控点资源状态");

            // 2.读取模板处理好样式

            // 3.转换成大数据读取模式
            swb = new SXSSFWorkbook(wb, 1000); // 用于大文件导出
            sheet = swb.getSheetAt(0);

            // 4.大批量写入数据

            // 5.保存到本地文件夹
            os = new FileOutputStream(new File(path));
            swb.write(os);

            return path;
        } catch (IOException e) {
            log.error("导出失败：" + e.getMessage());
            e.printStackTrace();
            return null;
        } catch (InvalidFormatException e) {
            log.error("导出失败：" + e.getMessage());
            e.printStackTrace();
            return null;
        } finally {
            close(os, swb, wb);
        }
    }
    /**
     * 资源关闭
     *
     * @param os
     * @param wb
     * @param swb
     */
    public static void close(FileOutputStream os, SXSSFWorkbook swb, XSSFWorkbook wb) {
        if (null != os) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != swb) {
            try {
                swb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != wb) {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
